/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.processing.prediction.recalc;

import aiaudio.lastfm.hbase.CannotCreateTableException;
import aiaudio.Application;
import aiaudio.processing.MainTableGroup;
import java.io.IOException;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.mapreduce.Job;

/**
 *
 * @author nastya
 */
public class UserRatingsCalculationAlgorithm{

    private final MainTableGroup mainTableGroup;

    public UserRatingsCalculationAlgorithm(MainTableGroup mainTableGroup) {
        this.mainTableGroup = mainTableGroup;
    }

    


    public void start() throws IOException, CannotCreateTableException, InterruptedException, ClassNotFoundException {
        Job j = createCalculationJob();
        j.waitForCompletion(true);
    }

    private Job createCalculationJob() throws IOException {
        Job job = new Job();

        job.getConfiguration().set(UserGroupAggregatedTableMapper.GROUP_TO_USER_MAP_NAME, mainTableGroup.getNetworkAlgTableGroup().getGroupToUserTable());
        job.getConfiguration().set(UserGroupRatingCalculationReducer.INITIAL_RATING_MATRIX, mainTableGroup.getSplitDataSetAlgTableGroup().getTrainingRatingMatrix());
        job.getConfiguration().set(UserGroupRatingCalculationReducer.RECOMENDATION_SET_LIST, mainTableGroup.getRatingPredictionAlgTableGroup().getRecommendationCreationRatingList());
        
        Scan scan = new Scan();
        
        String userToGroupTable = Application.database().getByName(mainTableGroup.getNetworkAlgTableGroup().getUserToGroupTable()).getDatabaseName();
        TableMapReduceUtil.initTableMapperJob(userToGroupTable, scan, UserGroupAggregatedTableMapper.class,
                ImmutableBytesWritable.class, UserGrouppingAggregate.class, job);
        
        String recCreation = Application.database().getByName(mainTableGroup.getRatingPredictionAlgTableGroup().getRecommendationCreationRatingList()).getDatabaseName();
        TableMapReduceUtil.initTableReducerJob(recCreation, UserGroupRatingCalculationReducer.class, job);
        return job;
    }

}
